﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace PM.PMSys
{
    /// <summary>
    /// 记录系统日志
    /// </summary>
    public class log
    {
        public log()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 插入日志
        /// </summary>
        /// <param name="content">日志内容</param>
        /// <param name="operatetype">日志类型</param>
        public void Insert_Log(string ocontent,int operatetype)
        {
            string tname = PM.Code.Basic.pmbasic.GetSessionByName("name");
            string gettype = PM.Code.Basic.pmbasic.GetSessionByName("usertype");
            int usertype = (gettype == string.Empty) ? 0 : Int32.Parse(gettype);
            string ip = HttpContext.Current.Request.UserHostAddress;
            string browser = HttpContext.Current.Request.Browser.Type;
            try
            {
                SqlParameter[] LogSqlParameter ={ 
                new SqlParameter("@username",SqlDbType.NVarChar),
                new SqlParameter("@usertype",SqlDbType.TinyInt),
                new SqlParameter("@ip",SqlDbType.NVarChar),
                new SqlParameter("@browser",SqlDbType.NVarChar),
                new SqlParameter("@operate",SqlDbType.NVarChar),
                new SqlParameter("@operatedate",SqlDbType.DateTime),
                new SqlParameter("@flag",SqlDbType.TinyInt)};
                LogSqlParameter[0].Value = tname;
                LogSqlParameter[1].Value = usertype;
                LogSqlParameter[2].Value = ip;
                LogSqlParameter[3].Value = browser;
                LogSqlParameter[4].Value = ocontent;
                LogSqlParameter[5].Value = DateTime.Now;
                LogSqlParameter[6].Value = operatetype;

                StringBuilder sqlstr = new StringBuilder("insert into Sys_Log(");
                sqlstr.Append("username,usertype,ip,browser,operate,operatedate,flag)");
                sqlstr.Append(" values(");
                sqlstr.Append("@username,@usertype,@ip,@browser,@operate,@operatedate,@flag)");
                PM.Code.DAL.DbHelper.ExecuteSql(sqlstr.ToString(), LogSqlParameter);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="databasename">数据库名称</param>
        /// <param name="datafilename">备份的文件名称</param>
        public void DataBackUp(string databasename,string datafilename )
        {
            string sqlstr="backup database @DataBaseName to disk = @BakFileName";
            SqlParameter[] parameters = {
                    new SqlParameter("@DataBaseName", SqlDbType.VarChar,100),
            new SqlParameter("@BakFileName", SqlDbType.VarChar,100)};
            parameters[0].Value = databasename;
            parameters[1].Value = datafilename;
            PM.Code.DAL.DbHelper.ExecuteSql(sqlstr, parameters);
        }
    }
}
